package http_servicer

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os/user"
)

func indexPageHandler(response http.ResponseWriter, request *http.Request) {
	fmt.Fprintf(response, loginPage)
	fmt.Fprintf(response, registerPage)
}

func testPageHandler(response http.ResponseWriter, request *http.Request) {
	// logger = log.New(os.Stdout, ">>>>>>> Test Page:", log.LstdFlags|log.Lshortfile)

	v_session_id, v_user_id := getAccountName(request)

	logger.Println(">>>session_id:", v_session_id, ", user_id :", v_user_id)

	if v_user_id == 0 {
		//reloginPage( response, request )
		fmt.Fprint(response, loginPage)
		fmt.Fprintf(response, registerPage)
	} else {

		switch request.FormValue("test") {

		case "test":
			fmt.Fprint(response, indexPage)
		case "user":
			fmt.Fprint(response, chgPasswordPage)

			fmt.Fprint(response, modiUserAddrPage)
			fmt.Fprint(response, fetchChildAddrPage)
			fmt.Fprint(response, fetchUserInfoPage)

			fmt.Fprint(response, modiUserAlertNumPage)
			fmt.Fprint(response, fetchContinentsPage)
			fmt.Fprint(response, fetchCountriesPage)

		case "pond":
			fmt.Fprint(response, newPondPage)
			fmt.Fprint(response, fetchpondPage)
			fmt.Fprint(response, modifypondPage)
			fmt.Fprint(response, revokepondPage)

		case "device":
			fmt.Fprint(response, addDevicePage)
			fmt.Fprint(response, modifyDevicePage)

			fmt.Fprint(response, deviceListPage)
			fmt.Fprint(response, setDeviceMarginPage)

			fmt.Fprint(response, getDeviceMarginPage)
			fmt.Fprint(response, cmdDeviceAlignPage)
			fmt.Fprint(response, fetchDeviceAlign)

		case "data":
			fmt.Fprint(response, realTimePondDataPage)
			fmt.Fprint(response, pondHistDataListPage)
			fmt.Fprint(response, pondHistDataDailyPage)
			fmt.Fprint(response, pondHistDataHourlyPage)

		case "alex":
			fmt.Fprint(response, viewLogsPage)

		case "logs":
			var fn string

			usr, err := user.Current()
			if err != nil {
				fmt.Fprintf(response, "Error in get current os user information: %v \n", err)
			} else {

				ht := request.FormValue("log") // http , tcp
				en := gUser                    // test, dev, prod, uat
				fn = fmt.Sprintf("%v/n_%v_%v_log", usr.HomeDir, ht, en)

				// fmt.Println("filename: " , fn )

				f, err := ioutil.ReadFile(fn)
				if err != nil {
					fmt.Fprintf(response, fmt.Sprintln(err))
				} else {

					fmt.Fprintln(response, string(f))
				}

			}

		default:
			fmt.Fprint(response, logoutPage)

		}

	}

	//  var responseStr []byte

}

const viewLogsPage = `
<h1> View Logs </h1>
<form method="post" action="/test">
    <label for="name">HTTP Logs </label>    <input hidden type="text" name="test" value="logs">
    <label for="name">log_file </label>    <input hidden type="text" name="log" value="http">

    <button type="submit"> >>>>> </button>
</form>
<form method="post" action="/test">
    <label for="name">TCP Logs </label>    <input hidden type="text"  name="test" value="logs">
    <label for="name">log_file </label>    <input hidden  type="text" name="log" value="tcp">
    <button type="submit"> >>>>>> </button>
</form>
</p>
<hr>
`

const indexPage = `
<h1>Test Index </h1>
<form method="post" action="/test">
    <label for="name">User  </label>    <input hidden type="text" name="test" value="user">
    <button type="submit"> >>>>> </button>
</form>
<form method="post" action="/test">
    <label for="name">Pond </label>    <input hidden type="text"  name="test" value="pond">
    <button type="submit"> >>>>>> </button>
</form>
<form method="post" action="/test">
    <label for="name">Device </label>    <input hidden type="text"  name="test" value="device">
    <button type="submit"> >>>>>> </button>
</form>

<form method="post" action="/test">
    <label for="name">Data </label>    <input hidden type="text"  name="test" value="data">
    <button type="submit"> >>>>>> </button>
</form>
</p>
<form method="post" action="/test">
    <label for="name">Logs </label>    <input hidden type="text"  name="test" value="alex">
    <button type="submit"> >>>>>> </button>
</form>
<hr>
`
